﻿using FineAdmin.IService;
using FineAdmin.Model;
using FineAdmin.Web.Controllers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace FineAdmin.Web.Areas.Blog.Controllers
{
    public class BlogPostsCategorieSetController : BaseController
    {
        // GET: Blog/BlogPostsCategorieSet

        public IBlogPostsCategorieService BlogPostsCategorieService { get; set; }
        public override ActionResult Index(int? id)
        {
            base.Index(id);
            return View();
        }

        [HttpGet]
        public JsonResult List()
        {
            var list = BlogPostsCategorieService.GetAll();
            var result = new { code = 0, count = list.Count(), data = list.OrderBy(m => m.SortCode).ToList() };
            return Json(result, JsonRequestBehavior.AllowGet);
        }
        [HttpGet]
        public JsonResult GetTreeSelect()
        {
            var result = BlogPostsCategorieService.GetTreeSelect();
            return Json(result, JsonRequestBehavior.AllowGet);
        }



        public ActionResult Add()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Add(BlogPostsCategorieModel model)
        {
            if (!model.PId.HasValue)
            {
                model.PId = 0;
            }
            model.CreateTime = DateTime.Now;
            model.CreateUserId = Operator.UserId;
            model.UpdateTime = DateTime.Now;
            model.UpdateUserId = Operator.UserId;
            var result = BlogPostsCategorieService.Insert(model) ? SuccessTip("添加成功") : ErrorTip("添加失败");
            return Json(result);
        }
        public ActionResult Edit(int id)
        {
            var model = BlogPostsCategorieService.GetById(id);
            return View(model);
        }
        [HttpPost]
        public ActionResult Edit(BlogPostsCategorieModel model)
        {
            if (!model.PId.HasValue)
            {
                model.PId = 0;
            }
            model.UpdateTime = DateTime.Now;
            model.UpdateUserId = Operator.UserId;
            var result = BlogPostsCategorieService.UpdateById(model) ? SuccessTip("修改成功") : ErrorTip("修改失败");
            return Json(result);
        }
        [HttpGet]
        public JsonResult Delete(int id)
        {
            var result = BlogPostsCategorieService.DeleteById(id) ? SuccessTip("删除成功") : ErrorTip("删除失败");
            return Json(result, JsonRequestBehavior.AllowGet);
        }
    }
}